﻿/*-----------------------------------------------
// File: BalanceBase.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


namespace Battle 
{
    public enum BalanceResult {
        Win,
        Lose,
    }

    public class BalanceBase : MonoBehaviour
    {
        public List<UIAtlas> usedAtlas;

        public GameObject winBase;
        public GameObject loseBase;
        public GameObject mask;
        public GameObject totalMask;
        public BalanceResult result;

        public GameObject commonIconPrefab;

        public List<UITweener> tweeners;


        public virtual void OnTotalMaskClick()
        {
            foreach (var tweener in tweeners)
            {
                if (tweener != null)
                {
                    tweener.PlayReverse();
                    tweener.ResetToBeginning();
                    tweener.enabled = false;
                }
            }
            totalMask.SetActive(false);
        }
        public void Show()
        {
            gameObject.SetActive(true);
            Refresh();
            AudioManager.StopMusic();
            StaticData.battleLeaveOption = BalanceLeaveOption.None;
        }

        public void Hide()
        {
            gameObject.SetActive(false);
        }

        public void Init(BalanceResult _result)
        {
            result = _result;
        }

        protected AudioSource audioSource;

        protected virtual void Refresh() {
            if (result == BalanceResult.Win)
            {
                winBase.gameObject.SetActive(true);
                loseBase.gameObject.SetActive(false);
                totalMask.SetActive(true);
                audioSource = AudioManager.Instance.PlayUIAudioWithReturn("allClear");
            }
            else
            {
                winBase.gameObject.SetActive(false);
                loseBase.gameObject.SetActive(true);
                totalMask.SetActive(false);
            }
            AddBalanceReward();
        }

        protected virtual object GetResultMsg()
        {
            return StaticData.lastBattleEndMsg;
        }

        public virtual void Exit() {

            if (audioSource != null)
            {
                audioSource.Stop();
                GameObject.Destroy(audioSource.gameObject);
            }

            foreach (var atlas in usedAtlas)
            {
                Resources.UnloadAsset(atlas.spriteMaterial.mainTexture);
            }

        }

        public void OnLotteryButtonClick()
        {
            StaticData.battleLeaveOption = BalanceLeaveOption.Lottery;
            Exit();
        }
        public void OnEquipStrengthButtonClick()
        {
            StaticData.battleLeaveOption = BalanceLeaveOption.EquipImprove;
            Exit();
        }
        public void OnHeroImproveButtonClick()
        {
            StaticData.battleLeaveOption = BalanceLeaveOption.HeroImprove;
            Exit();
        }

        public virtual void AddBalanceReward() { }

        public void OnMaskClick() {
            Exit();
        }

        public static int GetAwardThingNum(ProtoBuf.Message.ResultObject gain, string thingType) {
            if (gain != null) {
                return GetAwardThingNum(gain.simps, thingType);
            }
            return 0;
        }

        public static int GetAwardThingNum(List<ProtoBuf.Message.SimpleItem> simps, string thingType) {
            foreach (var item in simps) {
                if (item.id == thingType)
                    return item.num;
            }
            return 0;

        }

        public const string star_an_name = "UI_jiesuan_star_01_di";
        public const string star_liang_name = "UI_jiesuan_star_01";
    }
}
